/**
 */
package com.dingpeikeji.quickdeer.core.modules.schedef.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.modules.bus.entity.TCompBus;
import com.dingpeikeji.quickdeer.core.modules.bus.service.TCompBusService;
import com.dingpeikeji.quickdeer.core.modules.schedef.dao.TSchedefBoardDao;
import com.dingpeikeji.quickdeer.core.modules.schedef.dao.TSchedefDao;
import com.dingpeikeji.quickdeer.core.modules.schedef.dao.TSchedefOffDao;
import com.dingpeikeji.quickdeer.core.modules.schedef.dao.TSchedefVehiDao;
import com.dingpeikeji.quickdeer.core.modules.schedef.entity.TSchedef;
import com.dingpeikeji.quickdeer.core.modules.schedef.entity.TSchedefBoard;
import com.dingpeikeji.quickdeer.core.modules.schedef.entity.TSchedefOff;
import com.dingpeikeji.quickdeer.core.modules.schedef.entity.TSchedefVehi;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;

/**
 * 班次定义Service
 * 
 * @author Zhaoy
 * @version 2016-05-27
 */
@Service
@Transactional(readOnly = true)
public class TSchedefService extends CrudService<TSchedefDao, TSchedef> {

	@Autowired
	private TSchedefBoardDao tSchedefBoardDao;
	@Autowired
	private TSchedefOffDao tSchedefOffDao;
	@Autowired
	private TSchedefVehiDao tSchedefVehiDao;
	@Autowired
	private TCompBusService compBusService;

	public TSchedef get(String id) {
		TSchedef tSchedef = super.get(id);
		TSchedefBoard schedefBoard = new TSchedefBoard();
		TSchedefOff schedefOff = new TSchedefOff();
		TSchedefVehi schedefVehi = new TSchedefVehi();
		schedefBoard.setScheid(tSchedef.getScheid());
		schedefOff.setScheid(tSchedef.getScheid());
		schedefVehi.setScheid(tSchedef.getScheid());
		tSchedef.setTSchedefBoardList(tSchedefBoardDao.findList(schedefBoard));
		tSchedef.setTSchedefOffList(tSchedefOffDao.findList(schedefOff));
		tSchedef.setTSchedefVehiList(tSchedefVehiDao.findList(schedefVehi));
		return tSchedef;
	}

	public List<TSchedef> findList(TSchedef tSchedef) {
		return super.findList(tSchedef);
	}

	public Page<TSchedef> findPage(Page<TSchedef> page, TSchedef tSchedef) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()) {
			if (tSchedef.getOfficeid() == null)
				tSchedef.setOfficeid(user.getOffice().getId());
		}
		return super.findPage(page, tSchedef);
	}

	@Transactional(readOnly = false)
	public void save(TSchedef tSchedef) {
		tSchedef.setDefType(TSchedef.DEF_ORIGIN);
		if (StringUtils.isEmpty(tSchedef.getScheid())) {
			dao.insert(tSchedef);
		} else {
			dao.update(tSchedef);
		}
		// 2、操作上客点
		List<TSchedefBoard> tSchedefBoardList = tSchedef.getTSchedefBoardList();
		// 设置上客序号
		long boardNum = 0;
		if (tSchedefBoardList != null && !tSchedefBoardList.isEmpty()) {
			for (TSchedefBoard tSchedefBoard : tSchedef.getTSchedefBoardList()) {
				if (tSchedefBoard.getId() != null) {
					boardNum = boardNum + 1;
					tSchedefBoard.setSeqno(String.valueOf(boardNum));
					if (StringUtils.isEmpty(tSchedefBoard.getScheid())) {
						tSchedefBoard.setScheid(tSchedef.getScheid());
						tSchedefBoardDao.insert(tSchedefBoard);
					} else {
						tSchedefBoardDao.update(tSchedefBoard);
					}
				} else {
					tSchedefBoardDao.delete(tSchedefBoard);
				}
			}
		}
		// 下客点
		List<TSchedefOff> tschedefOffList = tSchedef.getTSchedefOffList();
		// 设置序号
		long offNum = 0;
		if (tschedefOffList != null && !tschedefOffList.isEmpty()) {
			for (TSchedefOff tSchedefOff : tSchedef.getTSchedefOffList()) {
				if (tSchedefOff.getRetailprice() == "" || tSchedefOff.getRetailprice().isEmpty())
					tSchedefOff.setRetailprice(null);
				if (tSchedefOff.getSettleprice() == "" || tSchedefOff.getSettleprice().isEmpty())
					tSchedefOff.setSettleprice(null);

				if (tSchedefOff.getId() != null) {
					offNum = offNum + 1;
					tSchedefOff.setSeqno(String.valueOf(offNum));
					if (StringUtils.isEmpty(tSchedefOff.getScheid())) {
						tSchedefOff.setScheid(tSchedef.getScheid());
						tSchedefOffDao.insert(tSchedefOff);
					} else {
						tSchedefOffDao.update(tSchedefOff);
					}
				} else {
					tSchedefOffDao.delete(tSchedefOff);
				}
			}
		}
		// 巴士车辆
		List<TSchedefVehi> tSchedefVehiList = tSchedef.getTSchedefVehiList();
		if (tSchedefVehiList != null && !tSchedefVehiList.isEmpty()) {
			for (TSchedefVehi tSchedefVehi : tSchedef.getTSchedefVehiList()) {
				if (tSchedefVehi.getId() != null) {
					// 通过车牌号 查询当前车辆信息
					TCompBus tCompBus = compBusService.get(tSchedefVehi.getBusid());
					tSchedefVehi.setLicno(tCompBus.getLicenseno());
					tSchedefVehi.setBrandname(tCompBus.getBrand());
					tSchedefVehi.setVicedriver(tCompBus.getVicedriver());
					tSchedefVehi.setMaindriver(tCompBus.getMaindriver());
					tSchedefVehi.setCargotypeid(String.valueOf(tCompBus.getCartype().getCargotypeid()));
					if (StringUtils.isEmpty(tSchedefVehi.getScheid())) {
						tSchedefVehi.setScheid(tSchedef.getScheid());
						tSchedefVehiDao.insert(tSchedefVehi);
					} else {
						tSchedefVehiDao.update(tSchedefVehi);
					}
				} else {
					tSchedefVehiDao.delete(tSchedefVehi);
				}
			}
		}
	}

	@Transactional(readOnly = false)
	public void dispatch(TSchedef tSchedef) {
		//调度记录同样写入T_Schedef,根据DefType区分，不修改原有班次定义信息
		
		//调度记录补充值
		tSchedef.setDefType(TSchedef.DEF_DISPATCH);
		tSchedef.setOriScheid(tSchedef.getScheid());
		tSchedef.setScheid("");
		dao.insert(tSchedef);

		// 2、操作上客点
		List<TSchedefBoard> tSchedefBoardList = tSchedef.getTSchedefBoardList();
		// 设置上客序号
		long boardNum = 0;
		if (tSchedefBoardList != null && !tSchedefBoardList.isEmpty()) {
			for (TSchedefBoard tSchedefBoard : tSchedef.getTSchedefBoardList()) {
				if (tSchedefBoard.getRetailprice() == "" || tSchedefBoard.getRetailprice().isEmpty())
					tSchedefBoard.setRetailprice(null);
				if (tSchedefBoard.getSettleprice() == "" || tSchedefBoard.getSettleprice().isEmpty())
					tSchedefBoard.setSettleprice(null);
				if (tSchedefBoard.getId() != null) {
					boardNum = boardNum + 1;
					tSchedefBoard.setSeqno(String.valueOf(boardNum));
					tSchedefBoard.setScheid(tSchedef.getScheid());
					tSchedefBoardDao.insert(tSchedefBoard);
				}
			}
		}
		// 下客点
		List<TSchedefOff> tschedefOffList = tSchedef.getTSchedefOffList();
		// 设置序号
		long offNum = 0;
		if (tschedefOffList != null && !tschedefOffList.isEmpty()) {
			for (TSchedefOff tSchedefOff : tSchedef.getTSchedefOffList()) {
				if (tSchedefOff.getRetailprice() == "" || tSchedefOff.getRetailprice().isEmpty())
					tSchedefOff.setRetailprice(null);
				if (tSchedefOff.getSettleprice() == "" || tSchedefOff.getSettleprice().isEmpty())
					tSchedefOff.setSettleprice(null);

				if (tSchedefOff.getId() != null) {
					offNum = offNum + 1;
					tSchedefOff.setSeqno(String.valueOf(offNum));
					tSchedefOff.setScheid(tSchedef.getScheid());
					tSchedefOffDao.insert(tSchedefOff);
				}
			}
		}
		// 巴士车辆
		List<TSchedefVehi> tSchedefVehiList = tSchedef.getTSchedefVehiList();
		if (tSchedefVehiList != null && !tSchedefVehiList.isEmpty()) {
			for (TSchedefVehi tSchedefVehi : tSchedef.getTSchedefVehiList()) {
				if (tSchedefVehi.getId() != null) {
					// 通过车牌号 查询当前车辆信息
					TCompBus tCompBus = compBusService.get(tSchedefVehi.getBusid());
					tSchedefVehi.setLicno(tCompBus.getLicenseno());
					tSchedefVehi.setBrandname(tCompBus.getBrand());
					tSchedefVehi.setVicedriver(tCompBus.getVicedriver());
					tSchedefVehi.setMaindriver(tCompBus.getMaindriver());
					tSchedefVehi.setCargotypeid(String.valueOf(tCompBus.getCartype().getCargotypeid()));
					tSchedefVehi.setScheid(tSchedef.getScheid());
					tSchedefVehiDao.insert(tSchedefVehi);
				}
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TSchedef tSchedef) {
		TSchedefBoard schedefBoard = new TSchedefBoard();
		TSchedefOff schedefOff = new TSchedefOff();
		TSchedefVehi schedefVehi = new TSchedefVehi();
		schedefBoard.setScheid(tSchedef.getScheid());
		schedefOff.setScheid(tSchedef.getScheid());
		schedefVehi.setScheid(tSchedef.getScheid());
		tSchedefBoardDao.delete(schedefBoard);
		tSchedefOffDao.delete(schedefOff);
		tSchedefVehiDao.delete(schedefVehi);
		super.delete(tSchedef);
	}

	/**
	 * 根据站点名称获取站点
	 * 
	 * @param siteanme
	 * @return
	 */
	public TSchedef getSchedefBySchename(String schename) {
		return dao.getSchedefBySchename(schename);
	}
}
